package com.atguigu.day03;

import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.typeinfo.TypeInformation;
import org.apache.flink.api.common.typeinfo.Types;
import org.apache.flink.api.connector.source.util.ratelimit.RateLimiterStrategy;
import org.apache.flink.connector.datagen.source.DataGeneratorSource;
import org.apache.flink.connector.datagen.source.GeneratorFunction;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

/**
 * @author Felix
 * @date 2024/7/10
 * 该案例演示了从数据生成器中读取数据
 */
public class Flink07_Source_DataGen {
    public static void main(String[] args) throws Exception {
        //TODO 1.指定流处理环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(2);
        //TODO 2.使用数据生成器生成数据
        DataGeneratorSource<String> dataGeneratorSource = new DataGeneratorSource<String>(
                new GeneratorFunction<Long, String>() {
                    @Override
                    public String map(Long value) throws Exception {
                        return "数据" + value;
                    }
                },
                100,
                RateLimiterStrategy.perSecond(10),
                Types.STRING
        );
        //TODO 3.封装为流
        DataStreamSource<String> ds = env.fromSource(dataGeneratorSource, WatermarkStrategy.noWatermarks(), "dataGen_source");
        //TODO 4.打印输出
        ds.print();
        //TODO 5.提交作业
        env.execute();
    }
}
